<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>16-cookie-封装</title>
		<script type="text/javascript">
			window.onload = function()
			{
				//document.cookie = "age = 99";
				
				//addCookie("gender","malexxx");
				
				addCookie("score","998",1,"/","127.0.0.1"); 
				function addCookie(key,value,day,path,domain)
				{
					//1.处理默认保存的路劲
					// if(!path)
					// {
					// 	var index = window.location.pathname.lastIndexOf("/");
					// 	var currentPath = window.location.pathname.slice(1, index);
					// 	path = currentPath;
						
					// }
					
						var index = window.location.pathname.lastIndexOf("/");
						var currentPath = window.location.pathname.slice(1, index);
						path = path || currentPath;
						
						//2.处理默认保存的domain
						domain = domain || document.domain;
						//3.处理默认的过期时间
						if(!day)
						{
							document.cookie = key+"="+value+";path="+path+";domain="+domain+";";
						}else
						{
							var date = new Date();
							date.setDate(date.getDate() + day);
							document.cookie = key+"="+value+";expires="+date.toGMTString()+";path="+path+";domain="+domain+";";
							
							
						}
						
						
			
					
					
				}
				
				//获取当前的url
				console.log(window.location.pathname);
				
				//lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置，在一个字符串中的指定位置从后向前搜索。
				// var index = window.location.pathname.lastIndexOf("/");
				// var currentPath = window.location.pathname.slice(1, index);
				
				// console.log(currentPath);
				
				console.log(document.domain)
			};
		
		</script>
	</head>
	<body>
	</body>
</html>
